package com.fangshicai.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.util.unit.DataSize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import javax.websocket.server.PathParam;
import java.io.File;
import java.io.IOException;

/**
 * @ClassName formController
 * @Description TODO
 * @Author fangshicai
 * @Date 2021/7/17 14:07
 */
@Slf4j
@Controller
public class formController {

    @GetMapping("/form_layouts")
    public String form_layoutsPage(){

        return "form/form_layouts";
    }

//    /**
//     * Max file size.
//     */
//    private DataSize maxFileSize = DataSize.ofMegabytes(1);
//
//    /**
//     * Max request size.
//     */
//    private DataSize maxRequestSize = DataSize.ofMegabytes(10);
    //文件上传功能
    @PostMapping("/upload")
    public String upload(@RequestParam("email") String email,
                         @RequestParam("password") String password,
                         @RequestPart MultipartFile headerImg,
                         @RequestPart MultipartFile[] photos) throws IOException {

        log.info("email={},password={},headerImg={},photos={}",email,password,headerImg.getSize(),photos.length);
        if(!headerImg.isEmpty()){
            String headerImgName = headerImg.getOriginalFilename();
//            System.out.println(headerImgName);
            headerImg.transferTo(new File("D:\\springboot\\"+headerImgName));
        }
        if(photos.length>0){
            for (MultipartFile photo : photos) {
                String photoName = photo.getOriginalFilename();
//                System.out.println(photoName);
                photo.transferTo(new File("D:\\springboot\\"+photoName));
            }
        }

        return "main";
    }

}
